//
//  HMNewViewController.m
//  网易彩票----2-24
//
//  Created by 唐后能 on 16/2/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "TJNewfeatureViewController.h"
#import "TJMainViewController.h"
#define NewPicCount 4

@interface TJNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageView;
@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation TJNewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // 创建scorollView,并往里面添加imgView,在最后一个imgView中加入按钮.
    [self addScrollView];
    // 创建pageView.
    [self addPageView];
}

// 创建scrollView.
- (void)addScrollView {

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:scrollView];
    
    CGFloat imgW = [UIScreen mainScreen].bounds.size.width;
    CGFloat imgH = [UIScreen mainScreen].bounds.size.height;
    for (int i = 0; i < NewPicCount; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.userInteractionEnabled = YES;
        CGFloat imgX = i*imgW;
        imgView.frame = CGRectMake(imgX, 0, imgW, imgH);
        NSString *imgName = [NSString stringWithFormat:@"new_feature_%d",i+1];
        imgView.image = [UIImage imageNamed:imgName];
        [scrollView addSubview:imgView];
        
        // 在最后一张图片中加入按钮.
        if (i==NewPicCount-1) {
            UIButton *startBtn = [[UIButton alloc]init];
            [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
            [startBtn setTitle:@"开始体验" forState:UIControlStateNormal];
            startBtn.bounds = CGRectMake(0, 0, startBtn.currentBackgroundImage.size.width, startBtn.currentBackgroundImage.size.height);
            startBtn.center = CGPointMake(self.view.center.x, self.view.bounds.size.height*0.8);
            [imgView addSubview:startBtn];
            //为startBtn添加点击事件.
            [startBtn addTarget:self action:@selector(startBtnClicked) forControlEvents:UIControlEventTouchDown];
        }
    }
    
    // 设置scrollView的其它属性.
    scrollView.contentSize = CGSizeMake(imgW*NewPicCount, 0);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    // 设置代理.
    scrollView.delegate = self;
    self.scrollView = scrollView;
    
}

// 创建pageView.
- (void)addPageView {

    UIPageControl *pageView = [[UIPageControl alloc]init];
    pageView.numberOfPages = NewPicCount;
    pageView.currentPageIndicatorTintColor = [UIColor redColor];
    pageView.pageIndicatorTintColor = [UIColor grayColor];
    pageView.bounds = CGRectMake(0, 0, 100, 30);
    pageView.center = CGPointMake(self.view.center.x, self.view.bounds.size.height*0.9);
    [self.view addSubview:pageView];
    self.pageView = pageView;
}


#pragma mark - 监听scrollViewde 滚动.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 计算当前滚动的页数.
    NSInteger currentPage = (self.scrollView.contentOffset.x + 0.5*scrollView.bounds.size.width )/scrollView.bounds.size.width;
    self.pageView.currentPage = currentPage;
}

#pragma mark - 按钮点击事件.
- (void)startBtnClicked {
    
    // 跳转到主界面.
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[TJMainViewController alloc] init];
    // 为切换界面时添加动画.
    [window.rootViewController.view addSubview:self.view];
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(-self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    }completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

// 验证是否有内存泄露.
- (void)dealloc {

    NSLog(@"死了");
}


@end
